/*
 * @Author: yuluo
 * @Date: 2025-08-28
 * @LastEditors: yuluo
 * @LastEditTime: 2025-09-06
 * @FilePath: \Projecte:\gitee\GD32F427\gd32-f427-BootLoader\BSP\src\drv_led.c
 * @Description:
 * 如有特殊注意事项请填写
 * Copyright (c) by ${yuluo}, All Rights Reserved.
 */

#include "main.h"

/* 绿色LED的操作指针 */
static io_operation_t drv_led_green  ={
    .init = drv_led_g_init,
    .io_disable = drv_led_g_close,
    .io_enable = drv_led_g_open,
};

io_device_t led_green = {
    .type = (const int8_t *)LED_G,
    .drv = &drv_led_green,
};

/**
 * @description: 绿色LED引脚初始化
 * @return {*}
 */
void drv_led_g_init(void)
{
    /* 使能GPIOF时钟 */
    rcu_periph_clock_enable(LED_GREEN_RCU);
    /* 设置GPIO输出模式和速度 */
    gpio_mode_set(LED_GREEN_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, LED_GREEN_PIN);
    gpio_output_options_set(LED_GREEN_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, LED_GREEN_PIN);
}

/**
 * @description: 绿色LED开启
 * @return {*}
 */
void drv_led_g_open(void)
{
    gpio_bit_reset(LED_GREEN_PORT, LED_GREEN_PIN);
}

/**
 * @description: 绿色LED关闭
 * @return {*}
 */
void drv_led_g_close(void)
{
    gpio_bit_set(LED_GREEN_PORT, LED_GREEN_PIN);
}

/* 红色LED的操作指针 */
static io_operation_t drv_led_red  ={
    .init = drv_led_r_init,
    .io_disable = drv_led_r_close,
    .io_enable = drv_led_r_open,
};

io_device_t led_red = {
    .type = (const int8_t *)LED_R,
    .drv = &drv_led_red,
};

/**
 * @description: 红色LED引脚初始化
 * @return {*}
 */
void drv_led_r_init(void)
{
    /* 使能GPIOF时钟 */
    rcu_periph_clock_enable(LED_RED_RCU);
    /* 设置GPIO输出模式和速度 */
    gpio_mode_set(LED_RED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, LED_RED_PIN);
    gpio_output_options_set(LED_RED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, LED_RED_PIN);
}

/**
 * @description: 红色LED开启
 * @return {*}
 */
void drv_led_r_open(void)
{
    gpio_bit_reset(LED_RED_PORT, LED_RED_PIN);
}

/**
 * @description: 红色LED关闭
 * @return {*}
 */
void drv_led_r_close(void)
{
    gpio_bit_set(LED_RED_PORT, LED_RED_PIN);
}

/**
 * @description: LED设备注册
 * @return {*}
 */
void led_register(void)
{
    io_device_register(&led_green);
    io_device_register(&led_red);
}
